function updateTimelineSQL_identica() {
	
	var conn = openDB();
	
	var username_identica = air.EncryptedLocalStore.getItem("username_identica");
	var password_identica = air.EncryptedLocalStore.getItem("password_identica");
	var tok = username_identica + ':' + password_identica;
	var hash = Base64.encode(tok);		
	var auth = "Basic " + hash;
	
	var request = new air.URLRequest("http://identi.ca/api/statuses/friends_timeline.xml");
	var header = new air.URLRequestHeader("Authorization", auth);
	air.URLRequestDefaults.userAgent = 'Twitch';
	request.method = air.URLRequestMethod.GET;
	request.requestHeaders.push(header);
	
	var loader = new air.URLLoader();
	loader.addEventListener(air.Event.COMPLETE, updateTimelineSQLHandler_identica);
	
	//TAE 2008.08.20 (start
	loader.addEventListener(air.IOErrorEvent.IO_ERROR, Bridge.ioErrorHandler);
	loader.addEventListener(air.HTTPStatusEvent.HTTP_RESPONSE_STATUS, Bridge.httpStatus_identica);
	//TAE 2008.08.20 (end

	try
	{
	    loader.load(request);
	} 
	catch (error)
	{
		dump('updateTimelineSQL : ' + error);
	    return;
	}
	
}

function updateTimelineSQLHandler_identica(event)
{ 	
	var username_identica = air.EncryptedLocalStore.getItem("username_identica");
	var conn = openDB();
	var dataXML = event.target.data;
	var parser = new DOMParser();
	var doc = parser.parseFromString(dataXML,"text/xml");
	
	dump('identica friends msg: ' + doc.getElementsByTagName('status').length);
	
	for (var i = 0; i < doc.getElementsByTagName('status').length; i++) {

		var twitter_id = '';
		var created_at = '';
		var text = '';
		var source = '';
		var real_name = '';
		var in_reply_to = '';
		var screen_name = '';
		var profile_image_url = '';
		
		//TAE 2008.08.22 (start
		var tweet_type = 'friend';
		var in_reply_status_id = 0;
		//TAE 2008.08.22 (end
		
		twitter_id = doc.getElementsByTagName('status')[i].getElementsByTagName('id')[0].childNodes(0).nodeValue;
		created_at = doc.getElementsByTagName('status')[i].getElementsByTagName('created_at')[0].childNodes(0).nodeValue;
		text = doc.getElementsByTagName('status')[i].getElementsByTagName('text')[0].childNodes(0).nodeValue;
		source = doc.getElementsByTagName('status')[i].getElementsByTagName('source')[0].childNodes(0).nodeValue;
		screen_name = doc.getElementsByTagName('status')[i].getElementsByTagName('user')[0].getElementsByTagName('screen_name')[0].childNodes(0).nodeValue;
		real_name = doc.getElementsByTagName('status')[i].getElementsByTagName('user')[0].getElementsByTagName('name')[0].childNodes(0).nodeValue;
		profile_image_url = doc.getElementsByTagName('status')[i].getElementsByTagName('user')[0].getElementsByTagName('profile_image_url')[0].childNodes(0).nodeValue;

		try {
			//Tae 2008.08.20 (start
			in_reply_status_id	=  doc.getElementsByTagName('status')[i].getElementsByTagName('in_reply_to_status_id')[0].childNodes(0).nodeValue;
			//Tae 2008.08.20 (end
		} catch (e) {
			//dump(e);
			//show for reply only...but I don't care it!
		}
		//TAE 2008.08.21 (start
		var t  			 = Date.parse(created_at);
		var created_time = parseInt(t);
		//TAE 2008.08.21 (end
		
		// Find and activate URL's
		var re = new RegExp(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ig);
		var matches = text.match(re);
		if (matches && matches.length) { 
			for (var x = 0; x < matches.length; x++) {
				var url = '<a href=# name="'+matches[x]+'" onclick="return gotolink()">'+matches[x]+'</a>';
				text = text.replace(matches[x], url);
			}
		}
		//TAE 2008.08.23 (start
		//Thanks to snitter
		if(text.match(new RegExp('@' + username_identica + '([^\w\d]|$)')))
        {
			tweet_type = 'replies';
        }
		
		text = setHashTag_identica(text);
		//TAE 2008.08.23 (End

		text = escape(text);
		
		// Find URL's in Source
		var re = new RegExp(/<a[^>]*>/ig);
		var matches = source.match(re);
		if (matches && matches.length) { 
			for (var x = 0; x < matches.length; x++) {
				var re2 = new RegExp(/\"[^\"]*\"/ig);
				var matches2 = matches[x].match(re2);
				if (matches2 && matches2.length) { 
					for (var y = 0; y < matches2.length; y++) {
						var from_link = matches2[y];
					}
				}
				var from_url = '<a href="#" name='+from_link+' onclick="return gotolink()">';
				source = source.replace(matches[x], from_url);
			}
		}

		// Check if twitter_id is in DB already
		var selectStmt = new air.SQLStatement();

		// A SQLConnection named "conn" has been created previously
		selectStmt.sqlConnection = conn;
		selectStmt.text = "SELECT twitter_id FROM timeline WHERE twitter_id = " + twitter_id;
		//RICHARD 2008.09.15 (START
		var insert = '0';

		try
		{	
		    selectStmt.execute();
			var result = selectStmt.getResult();
			dump('exists: ' + result.data[0].twitter_id);
		}
		catch (error)
		{
			dump('insert: ' + twitter_id);
		    insert = '1';
		}	

		if (insert == "1") 
		{
		
			// Insert new info into DB
			// create the SQL statement
			var insertStmt = new air.SQLStatement();
			insertStmt.sqlConnection = conn;

			// define the SQL text
			var sql = 
		    	"INSERT or REPLACE INTO timeline (twitter_id, screen_name, real_name, profile_image_url, created_at, text, source, type, service, in_reply_status_id, created_time) " + 
		    	"VALUES ('"+twitter_id+"', '"+screen_name+"', '"+real_name+"', '"+profile_image_url+"', '"+created_at+"', '"+text+"', '"+source+"', '" + tweet_type + "', 'identica', " + in_reply_status_id + "," + created_time +")";
			insertStmt.text = sql;
			try
			{	
			    insertStmt.execute();
				dump('insert successful');
			}
			catch (error)
			{
				dump(error);
			}
			
		
		}
		//RICHARD 2008.09.15 (END
	}

}

function updateRepliesTimelineSQL_identica() {

	var conn = openDB();
	
	var username_identica = air.EncryptedLocalStore.getItem("username_identica");
	var password_identica = air.EncryptedLocalStore.getItem("password_identica");
	var tok = username_identica + ':' + password_identica;
	var hash = Base64.encode(tok);		
	var auth = "Basic " + hash;
	
	var request = new air.URLRequest("http://identi.ca/api/statuses/replies.xml");
	air.URLRequestDefaults.userAgent = 'Twitch';
	var header = new air.URLRequestHeader("Authorization", auth);
	request.method = air.URLRequestMethod.GET;
	request.requestHeaders.push(header);
	
	var loader = new air.URLLoader();
	loader.addEventListener(air.Event.COMPLETE, updateRepliesTimelineSQLHandler_identica);

	//TAE 2008.08.20 (start
	loader.addEventListener(air.IOErrorEvent.IO_ERROR, Bridge.ioErrorHandler);
	loader.addEventListener(air.HTTPStatusEvent.HTTP_RESPONSE_STATUS, Bridge.httpStatus_identica);
	//TAE 2008.08.20 (end
	
	try
	{
	    loader.load(request);
	} 
	catch (error)
	{
	    return;
	}
}

function updateRepliesTimelineSQLHandler_identica(event)
{ 	
	var conn = openDB();
	var dataXML = event.target.data;
	var parser = new DOMParser();
	var doc = parser.parseFromString(dataXML,"text/xml");
	
	dump('reply msg: ' + doc.getElementsByTagName('status').length);
	
	for (var i = 0; i < doc.getElementsByTagName('status').length; i++) {
		var exists = '';
		var twitter_id = '';
		var created_at = '';
		var text = '';
		var source = '';
		var real_name = '';
		var in_reply_to = '';
		var screen_name = '';
		var profile_image_url = '';
		twitter_id = doc.getElementsByTagName('status')[i].getElementsByTagName('id')[0].childNodes(0).nodeValue;
		created_at = doc.getElementsByTagName('status')[i].getElementsByTagName('created_at')[0].childNodes(0).nodeValue;
		text = doc.getElementsByTagName('status')[i].getElementsByTagName('text')[0].childNodes(0).nodeValue;
		source = doc.getElementsByTagName('status')[i].getElementsByTagName('source')[0].childNodes(0).nodeValue;
		real_name = doc.getElementsByTagName('status')[i].getElementsByTagName('user')[0].getElementsByTagName('name')[0].childNodes(0).nodeValue;
		screen_name = doc.getElementsByTagName('status')[i].getElementsByTagName('user')[0].getElementsByTagName('screen_name')[0].childNodes(0).nodeValue;
		profile_image_url = doc.getElementsByTagName('status')[i].getElementsByTagName('user')[0].getElementsByTagName('profile_image_url')[0].childNodes(0).nodeValue;
	//Tae 2008.08.20 (start
		in_reply_status_id	=  doc.getElementsByTagName('status')[i].getElementsByTagName('in_reply_to_status_id')[0].childNodes(0).nodeValue;
	//Tae 2008.08.20 (end
	
		//TAE 2008.08.21 (start
		var t  			 = Date.parse(created_at);
		var created_time = parseInt(t);
		//TAE 2008.08.21 (end
	
		// Find and activate URL's
		var re = new RegExp(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ig);
		var matches = text.match(re);
		if (matches && matches.length) { 
			for (var x = 0; x < matches.length; x++) {
				var url = '<a href=# name="'+matches[x]+'" onclick="return gotolink()">'+matches[x]+'</a>';
				text = text.replace(matches[x], url);
			}
		}
		
		//TAE 2008.08.23 (start
		text = setHashTag_identica(text);
		//TAE 2008.08.23 (End
		
		text = escape(text);
		
		// Find URL's in Source
		var re = new RegExp(/<a[^>]*>/ig);
		var matches = source.match(re);
		if (matches && matches.length) { 
			for (var x = 0; x < matches.length; x++) {
				var re2 = new RegExp(/\"[^\"]*\"/ig);
				var matches2 = matches[x].match(re2);
				if (matches2 && matches2.length) { 
					for (var y = 0; y < matches2.length; y++) {
						var from_link = matches2[y];
					}
				}
				var from_url = '<a href="#" name='+from_link+' onclick="return gotolink()">';
				source = source.replace(matches[x], from_url);
			}
		}
		
		// Check if twitter_id is in DB already
		var selectStmt = new air.SQLStatement();

		// A SQLConnection named "conn" has been created previously
		selectStmt.sqlConnection = conn;
		selectStmt.text = "SELECT twitter_id FROM timeline WHERE twitter_id = " + twitter_id;
		//RICHARD 2008.09.15 (START
		var insert = '0';

		try
		{	
		    selectStmt.execute();
			var result = selectStmt.getResult();
			dump('exists: ' + result.data[0].twitter_id);
		}
		catch (error)
		{
			dump('insert: ' + twitter_id);
		    insert = '1';
		}	

		if (insert == "1") 
		{
		
			// Insert new info into DB
			// create the SQL statement
			var insertStmt = new air.SQLStatement();
			insertStmt.sqlConnection = conn;

			// define the SQL text
			var sql = 
			    "INSERT INTO timeline (twitter_id, screen_name, real_name, profile_image_url, created_at, text, source, type, service, in_reply_status_id, created_time) " + 
			    "VALUES ('"+twitter_id+"', '"+screen_name+"', '"+real_name+"', '"+profile_image_url+"', '"+created_at+"', '"+text+"', '"+source+"', 'replies', 'identica', " + in_reply_status_id + "," + created_time + ")";
			insertStmt.text = sql;
			insertStmt.execute();
		
		}
		//RICHARD 2008.09.15 (END
	}

}

//TAE 2008.08.17 (start
function updateDMTimelineSQL_identica() {

	var conn = openDB();
	
	var username_identica = air.EncryptedLocalStore.getItem("username_identica");
	var password_identica = air.EncryptedLocalStore.getItem("password_identica");
	var tok = username_identica + ':' + password_identica;
	var hash = Base64.encode(tok);		
	var auth = "Basic " + hash;
	
	var request = new air.URLRequest("http://identi.ca/api/direct_messages.xml");
	air.URLRequestDefaults.userAgent = 'Twitch';
	var header = new air.URLRequestHeader("Authorization", auth);
	request.method = air.URLRequestMethod.GET;
	request.requestHeaders.push(header);
	
	var loader = new air.URLLoader();
	loader.addEventListener(air.Event.COMPLETE, updateDMTimelineSQLHandler_identica);

	//TAE 2008.08.20 (start
	loader.addEventListener(air.IOErrorEvent.IO_ERROR, Bridge.ioErrorHandler);
	loader.addEventListener(air.HTTPStatusEvent.HTTP_RESPONSE_STATUS, Bridge.httpStatus_identica);
	//TAE 2008.08.20 (end
	
	try
	{
	    loader.load(request);
	} 
	catch (error)
	{
	    return;
	}
	
}

function updateDMTimelineSQLHandler_identica(event)
{ 	
	var conn = openDB();
	var dataXML = event.target.data;
	var parser = new DOMParser();
	var doc = parser.parseFromString(dataXML,"text/xml");
	
	dump('direct msg: ' + doc.getElementsByTagName('status').length);
	
	for (var i = 0; i < doc.getElementsByTagName('direct_message').length; i++) {
		var exists = '';
		var twitter_id = '';
		var created_at = '';
		var text = '';
		var source = '';
		var real_name = '';
		var in_reply_to = '';
		var screen_name = '';
		var profile_image_url = '';
		twitter_id = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('id')[0].childNodes(0).nodeValue;
		created_at = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('created_at')[0].childNodes(0).nodeValue;
		text = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('text')[0].childNodes(0).nodeValue;
		//source = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('source')[0].childNodes(0).nodeValue;
		source = 'direct message';
		real_name = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('sender')[0].getElementsByTagName('name')[0].childNodes(0).nodeValue;
		screen_name = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('sender')[0].getElementsByTagName('screen_name')[0].childNodes(0).nodeValue;
		profile_image_url = doc.getElementsByTagName('direct_message')[i].getElementsByTagName('sender')[0].getElementsByTagName('profile_image_url')[0].childNodes(0).nodeValue;

		//TAE 2008.08.21 (start
		var t  			 = Date.parse(created_at);
		var created_time = parseInt(t);
		//TAE 2008.08.21 (end
		
		// Find and activate URL's
		var re = new RegExp(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ig);
		var matches = text.match(re);
		if (matches && matches.length) { 
			for (var x = 0; x < matches.length; x++) {
				var url = '<a href=# name="'+matches[x]+'" onclick="return gotolink()">'+matches[x]+'</a>';
				text = text.replace(matches[x], url);
			}
		}
		
		//TAE 2008.08.23 (start
		text = setHashTag_identica(text);
		//TAE 2008.08.23 (End
		
		text = escape(text);
		
		// Find URL's in Source
		var re = new RegExp(/<a[^>]*>/ig);
		var matches = source.match(re);
		if (matches && matches.length) { 
			for (var x = 0; x < matches.length; x++) {
				var re2 = new RegExp(/\"[^\"]*\"/ig);
				var matches2 = matches[x].match(re2);
				if (matches2 && matches2.length) { 
					for (var y = 0; y < matches2.length; y++) {
						var from_link = matches2[y];
					}
				}
				var from_url = '<a href="#" name='+from_link+' onclick="return gotolink()">';
				source = source.replace(matches[x], from_url);
			}
		}
		
		// Check if twitter_id is in DB already
		var selectStmt = new air.SQLStatement();

		// A SQLConnection named "conn" has been created previously
		selectStmt.sqlConnection = conn;
		selectStmt.text = "SELECT twitter_id FROM timeline WHERE twitter_id = " + twitter_id;
		//RICHARD 2008.09.15 (START
		var insert = '0';

		try
		{	
		    selectStmt.execute();
			var result = selectStmt.getResult();
			dump('exists: ' + result.data[0].twitter_id);
		}
		catch (error)
		{
			dump('insert: ' + twitter_id);
		    insert = '1';
		}	

		if (insert == "1") 
		{
		
			// Insert new info into DB
			// create the SQL statement
			var insertStmt = new air.SQLStatement();
			insertStmt.sqlConnection = conn;

			// define the SQL text
			var sql = 
			    "INSERT INTO timeline (twitter_id, screen_name, real_name, profile_image_url, created_at, text, source, type, service, in_reply_status_id, created_time) " + 
			    "VALUES ('"+twitter_id+"', '"+screen_name+"', '"+real_name+"', '"+profile_image_url+"', '"+created_at+"', '"+text+"', '"+source+"', 'dm', 'identica', 0," + created_time +")";
			insertStmt.text = sql;
			insertStmt.execute();
		
		}
		//RICHARD 2008.09.15 (END
	}

}
//TAE 2008.08.17 (end

